#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;

namespace NativeTest_Question22
{
	typedef struct node
	{
		int val;
		node* Next;
	} node;

	node* FindLast(node* data, int n)
	{
		int len = 0;
		node* walker = data;
		while(walker && walker->Next )
		{
			len++;
			walker = walker->Next;
		}

		if(n > len -1) return NULL;

		int current = 0;
		while(data && data->Next )
		{
			if(current== len - (n-1))
			{
				return data;
			}
			current++;
			data = data->Next;
		}

		return NULL;
	}
}

namespace NativeTest
{
	using namespace NativeTest_Question22;
	TEST_CLASS(Question22)
	{
	public:

		TEST_METHOD(Question22_TestMethod1)
		{
			// TODO: Your test code here


			int n = 10;
			int a[] = {  3, 2, 1, 3, 5, 6, 2, 6, 3, 1};
			node* data, *head;

			for (int i = 0; i < n; i++)
			{
				node* tn = new node();
				tn->val= a[i];
				tn->Next = NULL;
				if(i == 0) { head = tn; data = tn;}
				else { data->Next = tn, data = tn;}
			}

			data = head;
			for (int i = 0; i < n; i++)
			{
				Assert::AreEqual(a[i], data->val);
				data = data->Next;
			}

			data = FindLast(head, 6);
			Assert::AreEqual(5, data->val);
		}

	};
}